package com.example.demo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Group;
import com.example.demo.entity.Shoppingcart;
import com.example.demo.service.GroupService;
import com.example.demo.service.ShoppingcartService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shoppingcart")
public class ShoppingcartController {
    @Resource
    ShoppingcartService shoppingcartService;

    //新增
    @PostMapping
    public Result<?> save(@RequestBody Shoppingcart shoppingcart) {
        if (null != shoppingcartService.selectById(shoppingcart.getFlowerId(),shoppingcart.getUserId())){
            shoppingcartService.updateNum(shoppingcart.getFlowerId(),shoppingcart.getUserId(), shoppingcart.getQuantity());
        }else{
            shoppingcartService.insert(shoppingcart);
        }
        return Result.success();
    }


    //修改
    @PutMapping("/quantity")
    public Result<?> update(@RequestBody Shoppingcart shoppingcart) {
        shoppingcartService.updateById(shoppingcart);
        return Result.success();
    }

    //删除
    @DeleteMapping("/{itemId}")
    public Result<?> delete(@PathVariable Long itemId) {
        shoppingcartService.deleteById(itemId);
        return Result.success();
    }


    //查询所有
    @GetMapping("/getAll")
    public Result<?> findAll(@RequestParam String userId) {
        List<Shoppingcart> shoppingcarts = shoppingcartService.selectByUserId(userId);
        return Result.success(shoppingcarts);
    }

    @GetMapping("/getChecked")
    public Result<?> findChecked(@RequestParam String userId) {
        List<Shoppingcart> shoppingcarts = shoppingcartService.selectChecked(userId);
        return Result.success(shoppingcarts);
    }

    @DeleteMapping("/delete")
    public Result<?> deleteByUserId(@RequestParam String userId){
        shoppingcartService.deleteByUserId(userId);
        return Result.success();
    }

}
